package com.digitalchina.lxnsy.activity.management;

import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseListActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.EnterpriselistVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.viewholder.ManagementListHolder;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/23 10:47
 * 修改备注：
 * ================================================
 */
public class ManagementListActivity extends BaseListActivity {

    private List<EnterpriselistVO> voList = new ArrayList<>();

    @Override
    protected void setAdapter() {

        setDecoration(2);

        setTitle("农企（合作社）列表");

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ManagementListHolder(parent);
            }
        });

        onRefresh();


        if (StringUtil.isStrEmpty(AdminXML.getUserAccId(activity))) {
            String userID = UserXML.getUserId(activity);
            AdminXML.setUserAccId(activity, userID);
        }
    }

    @Override
    protected void onClick(int position) {
        super.onClick(position);
        AdminXML.setEnterpriseId(activity, voList.get(position).getId());
        GotoUtil.gotoActivity(activity, ManagementDetailsV2Activity.class);
    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {

                if (recyclerView == null) {
                    return false;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_GET_ALL_ENTERPRISE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_ALL_ENTERPRISE_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }

                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "enterpriselist", EnterpriselistVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

//        if (voList != null) {
//            if (voList.size() < limit) {
        adapter.stopMore();
//            }
//        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAccount.getAllEnterprise(this, mHandler);
    }

    @Override
    public void onLoadMore() {
//        if (voList != null && voList.size() >= limit) {
//            page++;
//            BusinessAccount.getAllEnterprise(this, mHandler);
//        }
    }
}
